package com.stars.empoly.activity

import android.Manifest
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import com.baidu.mapapi.search.core.PoiInfo
import com.eazypermissions.common.model.PermissionResult
import com.eazypermissions.coroutinespermission.PermissionManager
import com.google.gson.Gson
import com.qmuiteam.qmui.kotlin.onClick
import com.stars.empoly.base.MBActivity
import com.stars.empoly.config.MIntentAction
import com.stars.empoly.databinding.ActivityAddressBinding
import com.stars.empoly.model.AddressList
import com.stars.empoly.vmodel.AddressVModel
import com.trailer.base.utils.UiSwitch
import kotlinx.coroutines.launch

/**
 * 添加地址
 *on 2022/9/24
 */
class AddressActivity : MBActivity<AddressVModel, ActivityAddressBinding>() {


    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)
        var data=intent.extras?.getString("bean")
        if(null!=data){
            val bean: AddressList = Gson().fromJson(data, AddressList::class.java)
            //设置数据
            vm.id=bean.id
            vm.name=bean.realName
            vm.phone=bean.phone
            vm.address=bean.address
            vm.latitude = bean.latitude
            vm.longitude =bean.longitude
            binding.tvAddress.text = bean.address
            vm.detailsAddress=bean.detailsAddress


        }
    }
    override fun listeners() {
        super.listeners()
        binding.llAddress.onClick {

            lifecycleScope.launch {
                if (PermissionManager.requestPermissions(
                        this@AddressActivity, 1000,
                        Manifest.permission.ACCESS_COARSE_LOCATION,
                        Manifest.permission.ACCESS_FINE_LOCATION,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_EXTERNAL_STORAGE
                    ) is PermissionResult.PermissionGranted
                ) {
                    UiSwitch.singleRes(
                        this@AddressActivity,
                        ChoseBaiDuActivity::class.java, MIntentAction.REQUEST_CODE_ONE
                    )
                }

            }
        }
    }

    override fun vmClass(): Class<AddressVModel> {
        return AddressVModel::class.java
    }

    override fun binding(): ActivityAddressBinding {
        return ActivityAddressBinding.inflate(layoutInflater)
    }

    override fun title(): String? {
        return "添加地址"
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode != Activity.RESULT_OK) {
            return
        }
        if (requestCode == MIntentAction.REQUEST_CODE_ONE) {
            val bundle = data!!.extras
            if (null != bundle) {
                var data = bundle.getString("bean")
                val item: PoiInfo = Gson().fromJson(data, PoiInfo::class.java)
                vm.address = item.address
                vm.latitude = item.getLocation().latitude.toString()
                vm.longitude = item.getLocation().longitude.toString()

                binding.tvAddress.text = item.address

            }


            return
        }

    }
}
